function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import { useCallback, useContext, useRef } from 'react';
import { BaseHelp, HelpContext, Table, TableSelectionModel } from "../../../functionalComponent";
import { Layout, iup, useRefCallback } from "../../../util";
import { findFilterIndexes } from "./utils";
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
var tableProps = {
  rowSelected: false,
  rowChecked: true,
  cache: false,
  headerMenu: false,
  style: {
    borderStyle: 'solid',
    borderColor: 'var(--border-color-split, #f0f0f0)',
    borderWidth: 1
  }
};
var tableStyle = {
  borderStyle: 'solid',
  borderColor: 'var(--border-color-split, #f0f0f0)',
  borderWidth: 1
};
var Header = BaseHelp.Header,
  Footer = BaseHelp.Footer,
  MultipleButtons = BaseHelp.MultipleButtons;

/**
 * 核算项目帮助-简单版
 * @returns
 */
export function HelpContentSimple() {
  var _useContext = useContext(HelpContext),
    request = _useContext.request,
    ok = _useContext.ok,
    _useContext$contentPa = _useContext.contentParams,
    getFieldValue = _useContext$contentPa.getFieldValue,
    columns = _useContext$contentPa.columns,
    helpTitle = _useContext$contentPa.helpTitle,
    helpId = _useContext$contentPa.helpId,
    selectedArray = _useContext$contentPa.selectedArray,
    multiple = _useContext$contentPa.multiple,
    footer = _useContext$contentPa.footer,
    randomKey = _useContext.randomKey;
  var btnRef = useRef();
  var rightTable = useRef();
  var SUB_TABLE_KEY = "".concat(helpId, "_").concat(randomKey, "_sub");

  // 表格
  var getTable = useCallback(function (tableKey) {
    return tableKey && iup.compIns[tableKey].getApi();
  }, []);
  var getResult = function getResult() {
    if (multiple) {
      var result = rightTable.current.getApi().getRows().map(function (r) {
        return {
          value: getFieldValue(r),
          label: getFieldValue(r, 'label'),
          origin: _objectSpread({}, r)
        };
      });
      return result.length > 0 ? result : undefined;
    } else {
      var value = getTable(SUB_TABLE_KEY).getSelectedData()[0];
      if (value) {
        return {
          value: getFieldValue(value),
          label: getFieldValue(value, 'label'),
          origin: _objectSpread({}, value)
        };
      }
      return undefined;
    }
  };
  var toRight = function toRight(values) {
    if (multiple) {
      btnRef.current.toRight(values);
    } else {
      ok(getResult());
    }
  };

  // 默认选中
  var onDataLoad = useRefCallback(function (table) {
    var _ref = table.getState() || {},
      _ref$dataSource = _ref.dataSource,
      dataSource = _ref$dataSource === void 0 ? [] : _ref$dataSource;
    var selectIndex = findFilterIndexes(dataSource, function (it) {
      return (selectedArray || []).map(function (x) {
        return x.id;
      }).includes(it.id);
    });
    table.setSelected(selectIndex);
  });
  return /*#__PURE__*/_jsxs(Layout, {
    children: [/*#__PURE__*/_jsx(Header, {
      title: helpTitle || '核算项目帮助'
    }), /*#__PURE__*/_jsxs(Layout.Flex, {
      direction: "row",
      style: {
        padding: 5
      },
      children: [/*#__PURE__*/_jsx(Layout.Flex, {
        direction: "column",
        children: /*#__PURE__*/_jsx(Table, {
          style: tableStyle,
          id: SUB_TABLE_KEY,
          columns: columns,
          pagination: {
            hideOnSinglePage: false
          },
          autoLoad: true,
          request: request,
          response: function response(res) {
            var _res$total;
            return {
              total: (_res$total = res.total) !== null && _res$total !== void 0 ? _res$total : 0,
              record: res.list || []
            };
          },
          onDataLoad: onDataLoad,
          rowSelection: {
            type: multiple ? [TableSelectionModel.CHECKBOX, TableSelectionModel.MULTIPLE_INTERVAL] : TableSelectionModel.ROW
          },
          onRow: function onRow(rowIndex, table) {
            return {
              onDoubleClick: function onDoubleClick() {
                return toRight([table.getRow(rowIndex)]);
              }
            };
          }
        })
      }), multiple && /*#__PURE__*/_jsx(MultipleButtons, {
        getRowKey: getFieldValue,
        outRef: btnRef,
        getActiveTable: function getActiveTable() {
          return getTable(SUB_TABLE_KEY);
        },
        getResultTable: function getResultTable() {
          return rightTable.current.getApi();
        }
      }), multiple && /*#__PURE__*/_jsx(Layout.Slider, {
        size: 200,
        children: /*#__PURE__*/_jsx(Table, _objectSpread(_objectSpread({}, tableProps), {}, {
          columns: columns,
          dataSource: selectedArray,
          ref: rightTable,
          onRow: function onRow(rowIndex) {
            return {
              onDoubleClick: function onDoubleClick() {
                return btnRef.current.toLeft(rowIndex);
              }
            };
          }
        }))
      })]
    }), /*#__PURE__*/_jsx(Footer, {
      getResult: getResult,
      children: footer && /*#__PURE__*/_jsx("span", {
        style: {
          flex: 1,
          width: 0
        },
        children: footer
      })
    })]
  });
}